package com.wgworkshop.pems.controller;

import com.wgworkshop.pems.common.ResponseData;
import com.wgworkshop.pems.model.WorkModel;
import com.wgworkshop.pems.model.WorkPreviewModel;
import com.wgworkshop.pems.model.WorkView;
import com.wgworkshop.pems.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作品控制器。
 * Created by wlc on 2017/8/30.
 */
@RestController
@RequestMapping("/works")
public class WorkController {
    @Autowired
    private WorkService workService;

    @PostMapping
    ResponseData<WorkModel> create(@RequestBody /*@Validated(CreateCheckGroup.class)*/ WorkModel workModel) {
        WorkModel resultModel = workService.save(workModel);
        return new ResponseData<WorkModel>().success(resultModel);
    }

    @DeleteMapping("/{id}")
    ResponseData<Void> delete(@PathVariable("id") Long id) {
        workService.delete(id);
        return new ResponseData<Void>().success();
    }

    @PatchMapping("/{id}")
    ResponseData<WorkModel> update(@PathVariable("id") Long id, @RequestBody String name) {
        WorkModel resultModel = workService.update(id, name);
        return new ResponseData<WorkModel>().success(resultModel);
    }

    @GetMapping("/week/{week}/num/{num}")
    ResponseData<WorkView> searchByWeekAndClassNum(@PathVariable("week") Integer week,
                                                   @PathVariable("num") Integer num,
                                                   @RequestParam(value = "shift", required = false, defaultValue = "0") Integer shift) {
        WorkView workView = workService.getWorkView(week, num, shift);
        return new ResponseData<WorkView>().success(workView);
    }

    @GetMapping("/preview/{week}/num/{num}")
    ResponseData<List<WorkPreviewModel>> searchWorkPreviews(@PathVariable("week") Integer week,
                                                            @PathVariable("num") Integer num) {
        List<WorkPreviewModel> workModels = workService.getWorkPreview(week, num);
        return new ResponseData<List<WorkPreviewModel>>().success(workModels);
    }
}
